//
//  AddressViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 3/9/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AddressViewController.h"
#import "ChangeInfoViewController.h"
#import "JSON.h"

@interface AddressViewController()

@property (nonatomic, retain) NSDictionary *addressDict;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@end

@implementation AddressViewController

@synthesize picker, myTableView;
@synthesize addressDict;
@synthesize province,city;

#pragma mark - AbstractTabViewController methods
- (void)rightItemClicked
{
    NSUInteger count = [[self.navigationController viewControllers] count];
    ChangeInfoViewController *changeController = (ChangeInfoViewController *)[[self.navigationController viewControllers] objectAtIndex:count-2];
    changeController.changeUser.userProvince = self.province;
    changeController.changeUser.userCity = self.city;
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - View lifecycle

- (void)dealloc
{
    [myTableView release];
    [province release];
    [city release];
    [addressDict release];
    [picker release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    NSInteger rowForOne = [[self.addressDict allKeys] indexOfObject:self.province];
    NSInteger rowForTwo = [[self.addressDict objectForKey:self.province] indexOfObject:self.city];
    
    [self.picker selectRow:rowForOne inComponent:0 animated:YES];
    [self.picker reloadComponent:1];
    [self.picker selectRow:rowForTwo inComponent:1 animated:YES];
//    [self.picker reloadAllComponents];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"选择地址";
    [self addNaviBarRightItemWithTitle:@"完成"];
    NSUInteger count = [[self.navigationController viewControllers] count];
    ChangeInfoViewController *changeController = (ChangeInfoViewController *)[[self.navigationController viewControllers] objectAtIndex:count-2];
    if(changeController.viewType == kViewEdit)
        [self setBackgroundImageViewForTabView];
    else
    {
        [self setBackgroundImageViewForRegisterView];
        //set approprite frame for subviews
        CGRect rect = self.myTableView.frame;
        rect.origin.y += 20.f;
        self.myTableView.frame = rect;
        rect = self.picker.frame;
        rect.origin.y += 49.f;
        self.picker.frame = rect;
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Provinces_Cities" ofType:@"txt"];
    NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    self.addressDict = [str JSONValue];
    [str release];
    self.province = changeController.changeUser.userProvince;
    self.city = changeController.changeUser.userCity;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.picker = nil;
    self.myTableView = nil;
}

#pragma mark - UIPickViewDatasource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
        return [[self.addressDict allKeys] count];
    NSInteger row = [pickerView selectedRowInComponent:0];
    NSString *key = [[self.addressDict allKeys] objectAtIndex:row];
    NSArray *values = [self.addressDict objectForKey:key];
    return [values count];
}
#pragma mark - UIPickViewDelegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
    {
        NSArray *keys = [self.addressDict allKeys];
        return [keys objectAtIndex:row];
    }
    NSInteger selectedRow = [pickerView selectedRowInComponent:0];
    NSString *key = [[self.addressDict allKeys] objectAtIndex:selectedRow];
    NSArray *values = [self.addressDict objectForKey:key];
    return [values objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0)
    {
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [self pickerView:pickerView didSelectRow:0 inComponent:1];
    }
    else
    {
        NSInteger rowForOne = [pickerView selectedRowInComponent:0];
        NSString *key = [[self.addressDict allKeys] objectAtIndex:rowForOne];
        NSString *value = [[self.addressDict objectForKey:key] objectAtIndex:row];
        self.province = key;
        self.city = value;
        [self.myTableView reloadData];
    }
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"请选择正确的省份城市";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"点击完成确认修改";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = self.province;
    cell.detailTextLabel.text = self.city;
    return cell;
}
@end
